package com.mobvel.zxstdzgl.utils;

import net.youmi.android.appoffers.AppOffersManager;
import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.admogo.AdMogoListener;
import com.mobclick.android.MobclickAgent;
import com.mobvel.zxstdzgl.R;
import com.mobvel.zxstdzgl.ui.MyDialog;
import com.waps.AppConnect;

public class AdTool implements AdMogoListener {
	
	private Context mContext;
	private LayoutInflater inflater;
//	private PreferencesHelper mHelper;
	
	private LinearLayout llAd;
	private com.admogo.AdMogoLayout adMogo;
	
	public AdTool(Context context) {
		mContext = context;
		
		inflater = ((Activity)mContext).getLayoutInflater();
//		mHelper = new PreferencesHelper(mContext);
		
		llAd = (LinearLayout)(((Activity)mContext).findViewById(R.id.ll_ad));
		adMogo = (com.admogo.AdMogoLayout)(((Activity)mContext).findViewById(R.id.admogo_layout));
		adMogo.setAdMogoListener(this);
		
		ImageButton ibCloseAd = (ImageButton)(((Activity)mContext).findViewById(R.id.ib_closead));
		ibCloseAd.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showCloseAdDialog();
			}
		});
		
		dealAd();
	}
	
	public void dealAd() {
		String adConfig = MobclickAgent.getConfigParams(mContext, "ad_switch");
		if(adConfig.equals("close")) {
			llAd.setVisibility(View.GONE);
		}else {
			llAd.setVisibility(View.VISIBLE);
		}
		
		int points = AppOffersManager.getPoints(mContext);
		if(points > 160) {
			llAd.setVisibility(View.GONE);
		}else {
			llAd.setVisibility(View.VISIBLE);
		}
	}
	
	public void showCloseAdDialog() {
		final MyDialog dialog = new MyDialog(mContext);
		View layTip = inflater.inflate(R.layout.closead_layout, null);
		
		final Button btn1 = (Button)layTip.findViewById(R.id.btn_1);
		btn1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		final Button btn2 = (Button)layTip.findViewById(R.id.btn_2);
		btn2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showOffers();
			}
		});
		
		dialog.setView(layTip);
		dialog.show();
	}
	
	public void showOffers() {
		String offerId = MobclickAgent.getConfigParams(mContext, "offer_id");
		if(offerId.equals("waps")) {
			//waps
			AppConnect.getInstance(mContext).showOffers(mContext);
		}else {
			//youmi
			AppOffersManager.showAppOffers(mContext);
		}
	}
	
	
	/*
	 * admogo 
	 * @see com.admogo.AdMogoListener#onClickAd()
	 */
	@Override
	public void onClickAd() {
		//收集广告点击事件
		String lable = null;
		if(null == adMogo.activeRation) {
			lable = "unknown";
		}else {
			lable = adMogo.activeRation.name;
			if(TextUtils.isEmpty(lable)) {
				lable = "unknown";
			}
		}
		
		MobclickAgent.onEvent(mContext, "ad_click", lable);
		llAd.setVisibility(View.GONE);
	}

	@Override
	public void onFailedReceiveAd() {
		String lable = null;
		if(null == adMogo.activeRation) {
			lable = "unknown";
		}else {
			lable = adMogo.activeRation.name;
			if(TextUtils.isEmpty(lable)) {
				lable = "unknown";
			}
		}
		
		MobclickAgent.onEvent(mContext, "ad_fail", lable);
	}

	@Override
	public void onReceiveAd() {
		//收集广告展示事件
		String lable = null;
		if(null == adMogo.activeRation) {
			lable = "unknown";
		}else {
			lable = adMogo.activeRation.name;
			if(TextUtils.isEmpty(lable)) {
				lable = "unknown";
			}
		}
		
		MobclickAgent.onEvent(mContext, "choose_ad", lable);
	}
	
	public void onCloseMogoDialog() {
		
	}

}
